/*
 * Copyright 2011-2012 by The Regents of the University of California Licensed
 * under the Apache License, Version 2.0 (the "License"); you may not use this
 * file except in compliance with the License. you may obtain a copy of the
 * License from
 * 
 * http://www.apache.org/licenses/LICENSE-2.0
 * 
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
 * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
 * License for the specific language governing permissions and limitations under
 * the License.
 */
package edu.ucsd.db.bassarisk.schema.visitor;

import java.util.List;

import edu.ucsd.db.bassarisk.schema.DisjointChildrenNode;
import edu.ucsd.db.bassarisk.schema.SchemaNode;
import edu.ucsd.db.bassarisk.schema.SchemaPositionAttributeNode;
import edu.ucsd.db.bassarisk.schema.SchemaRootNode;
import edu.ucsd.db.bassarisk.schema.SchemaRunAttributeNode;
import edu.ucsd.db.bassarisk.schema.SchemaRunNode;

public class SimplePathNavigator implements
    SchemaNodeVisitor<SchemaRunNode, Integer> {
  private final String[] simplePath;

  public SimplePathNavigator(final String[] simplePath) {
    this.simplePath = simplePath;
  }

  private SchemaRunNode visitChildren(final List<SchemaNode> children,
                                      final Integer idx) {
    SchemaRunNode result = null;
    for (SchemaNode child : children) {
      SchemaRunNode childResult = child.accept(this, idx + 1);
      if (childResult != null) {
        if (result != null) {
          // Simple paths should never have double matches in a well-formed
          // schema
          String message = "Invalid Schema.";
          throw new RuntimeException(message);
        }
        result = childResult;
      }
    }
    return result;
  }

  @Override
  public SchemaRunNode visitDisjointChildrenNode(final DisjointChildrenNode node,
                                                 final Integer idx) {
    List<SchemaNode> children = node.getChildren();
    return this.visitChildren(children, idx);
  }

  @Override
  public SchemaRunNode visitSchema(final SchemaRootNode node, final Integer idx) {
    List<SchemaNode> children = node.getChildren();
    return this.visitChildren(children, idx);
  }

  @Override
  public SchemaRunNode
      visitSchemaPositionAttributeNode(final SchemaPositionAttributeNode node,
                                       final Integer idx) {
    return null;
  }

  @Override
  public SchemaRunNode
      visitSchemaRunAttributeNode(final SchemaRunAttributeNode node,
                                  final Integer idx) {
    return null;
  }

  @Override
  public SchemaRunNode
      visitSchemaRunNode(final SchemaRunNode node, final Integer idx) {
    // check that this node matches specified path
    int checkingDepth = idx;
    String checkingTag = this.simplePath[checkingDepth];
    if (!checkingTag.equals(node.getName())) {
      // traversal does not match path;
      return null;
    }
    int nextDepth = checkingDepth + 1;
    // is path already fully satisfied?
    if (nextDepth == this.simplePath.length) {
      // traversal matches path
      return node;
    }
    // traversal matches prefix, recurse to children
    List<SchemaNode> children = node.getChildren();
    return this.visitChildren(children, idx);
  }
}